package mall.demo.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mall.demo.mybatis.pojo.Admin;
import mall.demo.mybatis.pojo.Order;
import mall.demo.mybatis.service.OrderService;
import mall.demo.util.OrderStatusEnum;
import mall.demo.util.OrderUtil;
import mall.demo.util.SessionKeyEnum;

@WebServlet("/updateOrderStatus.do")
public class UpdateOrderStatusServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Admin admin = (Admin)request.getSession().getAttribute(SessionKeyEnum.adminObj.name());
		if(admin == null) {
			response.sendRedirect(request.getContextPath() + "/login.jsp");
			return;
		}
		String orderId = request.getParameter("orderId");
		Order order = (new OrderService()).getOrderById(Integer.parseInt(orderId));
		if (order == null) {
			response.sendRedirect(request.getContextPath() + "/admin/order.jsp");
			return;
		}
		OrderStatusEnum newSt = OrderUtil.nextOrderStatus(
				OrderStatusEnum.valueOf(order.getStatus()));
		order.setStatus(newSt.name());
		order.setLastTime(new Date());
		(new OrderService()).updateOrderStatus(order);
		request.getRequestDispatcher("admin_order.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
